package com.taotao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.EasyUIResult;
import com.taotao.common.HttpClientUtil;
import com.taotao.common.TaotaoResult;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.service.ItemService;

@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;
	@Value("${SOLR_BASE_URL}")
	private String SOLR_BASE_URL;
	@Value("${SOLR_ITEM_CACHE_SYCN}")
	private String SOLR_ITEM_CACHE_SYCN;
	
	@RequestMapping("/item/{id}")
	@ResponseBody
	public TbItem showItem(@PathVariable Long id){
		return itemService.selectItem(id);
	}
	
	@RequestMapping("/item/list")
	@ResponseBody
	public EasyUIResult showItemList(@RequestParam(defaultValue="1") Integer page,
			@RequestParam(defaultValue="30") Integer rows){
		return itemService.getItemList(page, rows);
	}
	
	@RequestMapping("/item/save")
	@ResponseBody
	public TaotaoResult saveItem(TbItem item,String desc,@RequestParam("itemParams") String itemParam){
		TbItemDesc itemDesc=new TbItemDesc();
		itemDesc.setItemDesc(desc);
		TaotaoResult result=itemService.saveItem(item,itemDesc,itemParam);
		//同步solr缓存
		try {
			HttpClientUtil.doGet(SOLR_BASE_URL+SOLR_ITEM_CACHE_SYCN);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
